package com.yan_jiu_sheng.daily.AC;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/uOAnQW/description/?envType=daily-question&envId=2024-08-01
 * 自我AC通过
 *
 * @author yulongtian
 * @create 2024-08-01 9:08
 */
public class Test20240801 {
    public static void main(String[] args) {
//        int[] cards = {1, 2, 8, 9, 10};
        int[] cards = {3, 3, 1, 2};
//        int cnt = 3;
        int cnt = 3;
        System.out.println(new Test20240801().maxmiumScore(cards, cnt));
    }

    public int maxmiumScore(int[] cards, int cnt) {
        Arrays.sort(cards);

        int jiCnt = 0;
        int ouCnt = 0;
        int n = cards.length;
        for (int card : cards) {
            if ((card & 1) == 1) {//奇数
                jiCnt++;
            }
        }
        ouCnt = n - jiCnt;
        int[] jiArr = new int[jiCnt];
        int[] ouArr = new int[ouCnt];
        jiCnt = 0;
        ouCnt = 0;
        for (int i = n - 1; i >= 0; i--) {
            int card = cards[i];
            if ((card & 1) == 1) {
                jiArr[jiCnt++] = card;// 倒序
            } else {
                ouArr[ouCnt++] = card;// 倒序
            }
        }

        int ans = 0;
        ouCnt = 0;
        jiCnt = 0;
        int temp_cnt = 0;
        //先全拿偶数
        while (true) {
            if (temp_cnt >= cnt) {
                break;
            }
            if (ouCnt < ouArr.length) {
                ans += ouArr[ouCnt++];
                temp_cnt++;
            } else {
                if (jiCnt < jiArr.length && jiCnt + 1 < jiArr.length) {
                    ans += jiArr[jiCnt];
                    temp_cnt++;
                    if (temp_cnt >= cnt) {
                        // 全偶数 cnt剩一个，再拿两个奇数不行，超过cnt了，为了正好，去掉一个偶数，加上两个奇数
                        if (ouCnt == 0) {
                            return 0;
                        }
                        ouCnt--;
                        ans -= ouArr[ouCnt];
                        ans += jiArr[jiCnt + 1];
                        jiCnt += 2;
                        break;
                    }
                    ans += jiArr[jiCnt + 1];
                    temp_cnt++;
                    if (temp_cnt > cnt) {
                        ans -= jiArr[jiCnt + 1];
                        break;
                    }
                    jiCnt += 2;
                } else {
                    return 0;
                }
            }
        }
        // 拿偶数的最后两个换奇数的前两个大的
        while (true) {
            if (ouCnt - 1 >= 0 && ouCnt - 2 >= 0 && jiCnt < jiArr.length && jiCnt + 1 < jiArr.length) {
                int temp_ans = ans - ouArr[ouCnt - 1] - ouArr[ouCnt - 2];
                temp_ans = temp_ans + jiArr[jiCnt] + jiArr[jiCnt + 1];
                if (temp_ans > ans) {
                    ans = temp_ans;
                    ouCnt -= 2;
                    jiCnt += 2;
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        return ans;
    }

}
